<?php
use think\migration\Migrator;
use think\migration\db\table;

class SeedComputerQuestions extends Migrator
{
    public function up()
    {
        $time = time();
        $types = ['single','multi','judge','blank','short'];
    // delete existing questions of these types (use prefixed table name)
    $this->execute("DELETE FROM `ba_exam_question` WHERE `type` IN ('".implode("','", $types)."')");

        $rows = [];
        // helper to make option map
        $makeOptions = function(array $texts){
            $obj = [];
            foreach($texts as $i=>$t){
                $k = chr(65 + $i);
                $obj[$k] = $t;
            }
            return $obj;
        };

        // We'll prepare 50 for each type. For brevity we'll generate variations programmatically.
        // Single choice examples
        for($i=1;$i<=50;$i++){
            $opts = $makeOptions(["操作系统", "编程语言", "数据库", "网络协议"]);
            $rows[] = [
                'type'=>'single',
                'content'=>"单选：下面哪个是常见的网络传输层协议？ (#{$i})",
                'options'=>json_encode($opts, JSON_UNESCAPED_UNICODE),
                'answer'=>'A',
                'score'=>5,
                'analysis'=>'TCP是传输层协议，UDP也是传输层协议，常用于传输。',
                'create_time'=>$time,
                'update_time'=>$time
            ];
        }

        // Multi choice examples
        for($i=1;$i<=50;$i++){
            $opts = $makeOptions(["HTTP", "TCP", "UDP", "FTP"]);
            $rows[] = [
                'type'=>'multi',
                'content'=>"多选：下面哪些协议工作在传输层或以上？ (#{$i})",
                'options'=>json_encode($opts, JSON_UNESCAPED_UNICODE),
                'answer'=>'B,C',
                'score'=>5,
                'analysis'=>'TCP和UDP工作在传输层，HTTP和FTP工作在应用层。',
                'create_time'=>$time,
                'update_time'=>$time
            ];
        }

        // Judge examples
        for($i=1;$i<=50;$i++){
            $opts = $makeOptions(['对','错']);
            $rows[] = [
                'type'=>'judge',
                'content'=>"判断：Linux 是类 Unix 操作系统。 (#{$i})",
                'options'=>json_encode($opts, JSON_UNESCAPED_UNICODE),
                'answer'=>'A',
                'score'=>2,
                'analysis'=>'Linux 属于类 Unix 系统。',
                'create_time'=>$time,
                'update_time'=>$time
            ];
        }

        // Blank (fill in the blank) examples
        for($i=1;$i<=50;$i++){
            $rows[] = [
                'type'=>'blank',
                'content'=>"填空：HTTP 的默认端口是 ____ 。 (#{$i})",
                'options'=>null,
                'answer'=>'80',
                'score'=>3,
                'analysis'=>'HTTP 默认使用端口 80。',
                'create_time'=>$time,
                'update_time'=>$time
            ];
        }

        // Short answer examples
        for($i=1;$i<=50;$i++){
            $rows[] = [
                'type'=>'short',
                'content'=>"简答：简述进程与线程的区别。 (#{$i})",
                'options'=>null,
                'answer'=>'进程是资源分配的单位，线程是调度的单位，线程在进程内共享资源。',
                'score'=>10,
                'analysis'=>'进程与线程的区别主要在资源拥有与调度。',
                'create_time'=>$time,
                'update_time'=>$time
            ];
        }

        // Insert rows in batches
        $chunks = array_chunk($rows, 100);
        foreach($chunks as $chunk){
            $values = [];
            foreach($chunk as $r){
                $vals = [];
                foreach(['type','content','options','answer','score','analysis','create_time','update_time'] as $c){
                    if ($r[$c] === null) $vals[] = 'NULL';
                    else $vals[] = "'".addslashes($r[$c])."'";
                }
                $values[] = "(".implode(",", $vals).")";
            }
            $sql = "INSERT INTO `ba_exam_question` (`type`,`content`,`options`,`answer`,`score`,`analysis`,`create_time`,`update_time`) VALUES ".implode(',', $values);
            $this->execute($sql);
        }
    }

    public function down()
    {
    $types = ['single','multi','judge','blank','short'];
    $this->execute("DELETE FROM `ba_exam_question` WHERE `type` IN ('".implode("','", $types)."')");
    }
}
